Example 7
itoa()

 

 

Introduction:

In the standard C library, within stdlib.h, there is a call:
  extern int atoi(const char * /*nptr*/);
which tries to convert the string given to an integer representation.

There is no integer to string routine, as the ISO/ANSI people probably expected you to simply call printf(). However, if you are converting numbers to strings a lot, or if you do not readily have access to printf() (if, for example, you are writing your code in assembler!) then it can be a useful thing to have around.
Failing that, it is a good example...

 

 

How it works:

Basically, our integer is divided by ten, so we have a quotient and a remainder. And so long as the quotient is non-zero, we'll keep on going.

Our stack does not need checking. We use the variable registers v1 (R4) and v2 (R5) as working registers, to pass information to ourself each time around. And as we BL, we must also store lr (R14). The largest number that can be represented by an unsigned 32 bit integer is 4294967295 which is ten digits in length. So ten digits multiplied by three registers is 30, which equates to 120 bytes. An APCS conformant language will guarantee you 256 bytes of stack space, so we do not have a problem here.

 

 

[... to be finished (why are you reading this!?) ...]

 

 


Return to assembler index
Copyright © 2000 Richard Murray